﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SurveyAdd.ascx.cs" company="hoandd">
//   Do Duc Hoan
// </copyright>
// <summary>
//   DesktopModules Surveys SurveyAdd
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DesktopModules.Surveys
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI.WebControls;
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Localization;

    public partial class DesktopModules_Surveys_AddSurvey : PortalModuleBase
    {
        private int surveyID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request.QueryString["surveyID"] != null)
            {
                int.TryParse(Request.QueryString["surveyID"].ToString(), out surveyID);
            }
            if (!IsPostBack)
            {
                if (surveyID > 0)
                {
                    SurveyInfo objSurvey = SurveyColtroller.GetSurveys(surveyID);
                    txtSurvey.Text = objSurvey.Title;
                    txtDescription.Text = objSurvey.Description;
                    if (objSurvey.Public)
                        ckbpublic.Checked = true;
                    else
                        ckbpublic.Checked = false;
                }
            }
        }

        protected void btnAdd1_Click(object sender, EventArgs e)
        {
            // SurveyColtroller surveycontroller = new SurveyColtroller();
            SurveyInfo surveyinfo = new SurveyInfo();
            surveyinfo.OrgID = 4;
            surveyinfo.Title = txtSurvey.Text;
            surveyinfo.Description = txtDescription.Text;
            surveyinfo.CreatedByUserID = UserId;

            if (ckbpublic.Checked)
                surveyinfo.Public = true;
            else
                surveyinfo.Public = false;
            if (surveyID > 0)
            {
                surveyinfo.DateCreated = SurveyColtroller.GetSurveys(surveyID).DateCreated;
                surveyinfo.LastModified = DateTime.Now;
                surveyinfo.ModifiedByUserID = UserId;
                SurveyColtroller.UpdateSurveys(surveyinfo);
                divMessage.Visible = true;
                lblMessage.Text = "Cập nhật thành công !";
            }
            else
            {
                surveyinfo.DateCreated = DateTime.Now;
                surveyinfo.LastModified = DateTime.Now;
                surveyID = SurveyColtroller.AddSurveys(surveyinfo);
                divMessage.Visible = true;
                lblMessage.Text = "Thêm mới thành công !";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(TabId, "LIST_SURVEY", "mid=" + ModuleId));
        }

        protected void btnGanCauHoi_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(TabId, "SURVEY_ADD_QUESTION", "mid=" + ModuleId, "surveyID=" + surveyID));
        }
    }
}